package com.tinckay.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinckay.common.constant.AmqpConstant;
import com.tinckay.common.constant.RedisConstant;
import com.tinckay.common.util.LocalDateTimeUtils;
import com.tinckay.entity.po.Storehouse;
import com.tinckay.mapper.StorehouseMapper;
import com.tinckay.service.StorehouseService;
import cn.hutool.core.util.StrUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jean
 * @since 2018-10-21
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class StorehouseServiceImpl extends ServiceImpl<StorehouseMapper, Storehouse> implements StorehouseService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable code) {
        boolean result = super.remove(new QueryWrapper<Storehouse>().lambda().eq(Storehouse::getCode, code));
        redisTemplate.opsForHash().delete(RedisConstant.STOREHOUSE_MAP, code);
        sendMsg();
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Storehouse storehouse) {
        if (null == storehouse.getState()) {
            storehouse.setState(0);
        }
        if (StrUtil.isBlank(storehouse.getPipeCode())) {
            storehouse.setPipeCode("GJ-001");
        }
        storehouse.setCode(LocalDateTimeUtils.formatTime(LocalDateTime.now(), LocalDateTimeUtils.DTF_THREE));
        boolean result = super.save(storehouse);
        //cacheAndSendMsg();
        sendMsg();
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(Storehouse storehouse) {
        boolean result = super.updateById(storehouse);
        cacheAndSendMsg();
        return result;
    }

    @Override
    public Storehouse getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public List<String> findCode(String code) {
        return baseMapper.findCode(code);
    }

    public void cacheAndSendMsg() {
        //buildStorehouse();
        //redisTemplate.opsForHash().putAll(RedisConstant.STOREHOUSE_MAP, key, sotrehouse);
        sendMsg();
    }

    private void sendMsg() {
        rabbitTemplate.convertAndSend(AmqpConstant.REDIS_CHANGE_FANOUT_EXCHANGE,
                null,
                RedisConstant.STOREHOUSE_MAP,
                new CorrelationData(RandomUtil.simpleUUID()));
    }

    //private Map<String, com.tinckay.common.object.Storehouse> buildStorehouse(List<Storehouse> cacheList) {
    //    Map<String, com.tinckay.common.object.Storehouse> cacheMap = cacheList.parallelStream().map(storehouse -> {
    //        com.tinckay.common.object.Storehouse result = new com.tinckay.common.object.Storehouse();
    //        result.setId(storehouse.getId());
    //        result.setPipeCode(storehouse.getPipeCode());
    //        result.setCode(storehouse.getCode());
    //        result.setType(storehouse.getType() == null ? 0 : storehouse.getType());
    //        result.setLength(storehouse.getLength() == null ? 0 : storehouse.getLength());
    //        result.setWidth(storehouse.getWidth() == null ? 0 : storehouse.getWidth());
    //        result.setHeight(storehouse.getHeight() == null ? 0 : storehouse.getHeight());
    //        result.setCubic(storehouse.getCubic() == null ? 0 : storehouse.getCubic());
    //        result.setState(storehouse.getState());
    //        result.setWallThickness(storehouse.getWallThickness() == null ? 0 : storehouse.getWallThickness());
    //        return result;
    //    }).collect(Collectors.toMap(com.tinckay.common.object.Storehouse::getCode, Function.identity()));
    //    return cacheMap;
    //}

}
